/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrame.java
 *
 * Created on 12-mar-2010, 15.09.48
 */

package map.mapEditor;

import java.awt.Dimension;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import BothSide.Constants;


import java.io.PrintWriter;



/**
 *
 * @author iamdelac
 */
public class PutObstacles extends javax.swing.JFrame { 
 private final String OCCUPATO=Constants.OCCUPATO;
 private final String LIBERO=Constants.LIBERO;
 private final String SELEZIONATO=Constants.SELEZIONATO;
 private final int righe = Constants.righe; //j
 private final int colonne = Constants.colonne;//i
 EditorMattonella[][] mattonelle = new EditorMattonella[colonne][righe];
    /** Creates new form NewJFrame */
    public PutObstacles() {
      //  initComponents();
        initCustomComponents();

    }






    private void initCustomComponents(){
        String ostacoli = leggiOstacoli();
        background = new ImageIcon(getClass().getResource(File.separator+"map"+File.separator+"maps"+File.separator+"bk.jpg"));
        Dimension dim = new Dimension( background.getIconWidth()+getInsets().left+getInsets().right, background.getIconHeight()+getInsets().top+getInsets().bottom) ;
        JPanel panel = new JPanel()
            {@Override
             protected void paintComponent(Graphics g)
                {g.drawImage(background.getImage(), 0, 0, null); //full size image
                 //Dimension d = getSize();  //scale at panel size
                 //g.drawImage(background.getImage(), 0, 0, d.width, d.height, null);
                 super.paintComponent(g);
                }
            };
        panel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        panel.setLayout(null);
        panel.setOpaque(false);
        panel.setLocation(0, 100);
        
        panel.setPreferredSize(dim);
	setResizable(true);
        scrollPane = new JScrollPane(panel);
        scrollPane.getHorizontalScrollBar().setEnabled(false);
        scrollPane.getVerticalScrollBar().setEnabled(false);
        getContentPane().add(scrollPane);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setSize(500,300);


        int offset_x=0;
        for(int i=0; i < colonne; i++) //colonna
            {
             for(int j=0; j < righe; j++)  //riga
                 {
                    if (j%2 == 0 )
                         {offset_x = -(new javax.swing.ImageIcon(getClass().getResource(LIBERO)).getIconWidth()/2);}
                     else
                        {offset_x=0;}
                    boolean libero;
                    if (ostacoli != null)
                        {
                         if(ostacoli.charAt(j+(i*righe)) == "0".charAt(0)) {libero= true;}
                         else{libero= false;}
                        }
                    else{libero = true;}
                    EditorMattonella m = new EditorMattonella(libero);
                    mattonelle[i][j] = m;
                    m.setLocation((i*m.getIcon().getIconWidth())+offset_x, j*m.getIcon().getIconHeight()/2);
                    panel.add(m);
                 }
            }



        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt)
              {
                String toWrite="";
                for (int i=0; i<colonne;i++)
                    {
                      for (int j=0; j<righe;j++)
                          {
                           if (mattonelle[i][j].libero)
                               {toWrite=toWrite+"0";}
                           else{toWrite=toWrite+"1";System.out.println("ostacolo al rombo "+((Constants.righe*i)+j));}
                          }
                    }
                try{
                     String fileName  = (getClass().getResource(File.separator+"map"+File.separator+"maps"+File.separator+"bk.jpg").getFile()).replaceAll("jpg", "txt");
                     FileWriter outputFileReader  = new FileWriter(fileName);
                     PrintWriter    outputStream  = new PrintWriter(outputFileReader);
                     outputStream.println(toWrite);
                     outputStream.close();
                    }
                catch (Exception e){System.err.println("Error: " + e.getMessage());}
              }

        });
    }





private String leggiOstacoli(){
        String ostacoli = "";
        try{
            new File((getClass().getResource("/map/maps/bk.jpg").getFile()).replaceAll("jpg", "txt")).createNewFile();
            String fileName  = (getClass().getResource("/map/maps/bk.jpg").getFile()).replaceAll("jpg", "txt");
            FileReader inputFileReader   = new FileReader(fileName);
            BufferedReader inputStream   = new BufferedReader(inputFileReader);
            String inLine = null;
            ostacoli = inputStream.readLine();
            inputStream.close();
            }
        catch(Exception ex){ex.printStackTrace();}
        return ostacoli;
}



    /**
    * @param args the command line arguments
    */
    public  static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PutObstacles().setVisible(true);


            }
        });
    }




    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    private ImageIcon background;
    private JScrollPane scrollPane;




    private class EditorMattonella extends javax.swing.JLabel{
        boolean libero;        
        public EditorMattonella(boolean stato){
            super();            
            libero=stato;
            disegna(stato);
            this.setSize(this.getIcon().getIconWidth(), this.getIcon().getIconHeight());
            addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                if (libero)
                    {
                     disegna(false);
                    }
                else{
                     disegna(true);
                }
            }
            });
            addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                setIcon(new javax.swing.ImageIcon(getClass().getResource(SELEZIONATO)));
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                disegna(libero);
            }
            });
        }

        private void disegna(boolean libero){

                if (!libero)
                    {
                     this.libero=false;
                     setIcon(new javax.swing.ImageIcon(getClass().getResource(OCCUPATO)));
                    }
                else{
                     this.libero=true;
                     setIcon(new javax.swing.ImageIcon(getClass().getResource(LIBERO)));
                }
            }
        }
    }

